define(['./../initial', './../service/viewbag'], function () {
    var app = angular.module('App');
    app.factory('LocationPoints', ['$q', '$http', '$rootScope', 'viewbag',
        function ($q, $http, $rootScope, viewbag) {



            var LocationPoints = function () {
                this.allTimes = [];
                this.recentChanges = null;
                this.points = [];
                this.location = "";
                this.autoSurveyPrjId = "";
                this.autoSurveyPrjName = "";
                this.isAutoSurveyPrj = false;
                this.autoSurveyPrjOnline = false;
                this.selectedDates = []; //默认选择的兴趣时间列表
                this.isDesignMode = false;
                this.isPeriodMode = false;
                this.tempChangeDatas = {};//保存查询结果,维护一个结果对象
                this.isRingNumberConfig = true; //是否配置了里程和环号,并且可以在地图上显示
                this.isMileageConfig = true; //是否配置了里程和环号,并且可以在地图上显示
            };


            /** 是否应该按照里程来绘制图表 */
            LocationPoints.prototype.shouldDrawByMile = function (type) {
                if (viewbag.curType.typeCode === 'LongTermSubElevation' || viewbag.curType.typeCode === 'CriticalSectionMonitor') {
                    return type === '沉降' && this.isMileageConfig;
                }
                return false;
            }

            /** 是否配置环号 */
            LocationPoints.prototype.checkIsRingNumberConfig = function () {
                var _this = this;
                _this.isRingNumberConfig = !_this.points.some(function (pt) {
                    return pt.ringNumber === 'none';
                });
                return _this.isRingNumberConfig;
            };

            /** 是否配置里程 */
            LocationPoints.prototype.checkIsMileageConfig = function () {
                var _this = this;
                _this.isMileageConfig = _this.points.every(function (pt) {
                    return (pt.mileage - 0) > 0;
                });
                return _this.isMileageConfig;
            };

            /** 把里程赋值成字符串:如 1200 为 1Sk+200  */
            LocationPoints.prototype.parseMileageFormat = function (mileage) {
                var _this = this;
                var code = "K";
                switch (_this.location) {
                    case "上行线":
                        code = "SK";
                        break;
                    case "下行线":
                        code = "XK";
                        break
                    default:
                        break;
                }
                var num = mileage - 0;
                var k = Math.floor(mileage / 1000);
                return code + k.toString() + '+' + ('00' + (num - k * 1000).toFixed(1)).slice(-5);
            };

            /** 计算阶段变化 */
            function processPeriodChanges(pl) {
                var periodChange = pl.changes.lastValues.map(function (item, index) {
                    //为上次系列添加环号和特殊位置标志
                    item.ringNumber = pl.changes.curValues[index].ringNumber;
                    item.mileage = pl.changes.curValues[index].mileage;
                    item.mileageNumber = pl.changes.curValues[index].mileageNumber;
                    var value = 0;
                    if (item.value === null || pl.changes.curValues[index].value === null) {
                        value = null;
                    } else {
                        value = pl.changes.curValues[index].value - item.value;
                    }
                    var periodItem = angular.copy(item);
                    periodItem = angular.extend(periodItem, {
                        time: item.time + '至' + pl.changes.curValues[index].time,
                        value: value,
                        remark: pl.points[index].remark || ""
                    });
                    return periodItem;
                });

                //赋值阶段变形
                pl.tempChangeDatas = {
                    dataList: [],
                    max: null,
                    min: null,
                    drawByMile: pl.changes.drawByMile
                };
                pl.tempChangeDatas.dataList = [periodChange];
                pl.tempChangeDatas.title = pl.changes.title + '-阶段变化'
            }

            /** 
             * 第一次查询该monitorType,该location下的变化
             */
            LocationPoints.prototype.getRecentChanges = function (queryParams) {
                this.isPeriodMode = false;
                var defer = $q.defer();
                var ll = this;
                var url = '/metro/project/recentChanges';
                $http({
                    url: url,
                    method: 'get',
                    params: queryParams
                }).success(function (data) {
                    if (ll.shouldDrawByMile(queryParams.type)) {
                        if (data.curValues[1].mileage < data.curValues[0].mileage) {
                            data.curValues.reverse();
                            data.lastValues.reverse();
                        }
                        data.drawByMile = true;
                    } else {
                        data.drawByMile = false;
                    }
                    ll.changes = data;
                    ll.allTimes = data.allTimes;
                    //判断报警
                    ll.changes.curValues.forEach(function (item, index) {
                        if (Math.abs(item.value) >= 5) {
                            ll.points[index].alert = true;
                        } else {
                            ll.points[index].alert = false;
                        }
                        //为本次系列添加环号和特殊位置标志
                        item.remark = ll.points[index].remark || "";
                        item.ringNumber = (item.ringNumber - 0) > 0 ? (item.ringNumber - 0) : "无";
                        item.mileageNumber = item.mileage;//里程数字
                        item.mileage = (item.mileage - 0) > 0 ? ll.parseMileageFormat(item.mileage) : "无";//里程字符串
                    });
                    processPeriodChanges(ll);
                    defer.resolve(ll.changes);
                });
                return defer.promise;
            };

            // /** 给出2个日期,查询这2个日期当中的变化量,模糊查询 */
            // LocationPoints.prototype.getRecentFuzzyChanges = function (prjId, monitorType, clickDates, location, fuzzy) {
            //     this.isPeriodMode = false;
            //     fuzzy = fuzzy || 0;
            //     location = location || this.location;

            //     //default params
            //     var defer = $q.defer();
            //     var ll = this;
            //     $http({
            //         url: "/recentChanges/" + prjId.toString() + "/" + monitorType + "/" + Base64.encodeURI(ll.location) + "/" + clickDates[0] + "/" + clickDates[1] + "/" + fuzzy,
            //         method: 'get'
            //     }).success((function (pl) {
            //         return function (data) {
            //             defer.resolve(data);
            //         };
            //     })(ll));
            //     return defer.promise;
            // };

            // /** 给出一组用户点击的时间数组(2个以上),查询这些时间列表内的变化 */
            // LocationPoints.prototype.getRecent2Changes = function (prjId, monitorType, clickDates, location, fuzzy) {
            //     this.isPeriodMode = false;
            //     fuzzy = fuzzy || 0;
            //     location = location || this.location;
            //     this.selectedDates = clickDates;
            //     //default params
            //     var defer = $q.defer();
            //     var ll = this;
            //     $http({
            //         url: "/recentChangesByTimeList/" + prjId.toString() + "/" + monitorType + "/" + Base64.encodeURI(ll.location) + "/" + clickDates.join(",") + "/" + fuzzy + "/" + this.isDesignMode,
            //         method: 'get'
            //     }).success((function (pl) {
            //         return function (data) {
            //             if (viewbag.drawByMile && pl.points[0].mileage && monitorType === '沉降') {
            //                 if (pl.points[0].mileage > pl.points[1].mileage) {//倒序里程
            //                     data.dataList.forEach(function (list) {
            //                         list.reverse();
            //                     });
            //                 }
            //                 data.drawByMile = true;
            //             } else {
            //                 data.drawByMile = false;
            //             }
            //             for (var i = 0; i < data.dataList.length; i++) {
            //                 var periodChange = data.dataList[i].map(function (item, index) {
            //                     item.remark = (i === 0 && pl.points[index].remark) || "";
            //                     item.ringNumber = (pl.points[index].ringNumber - 0) > 0 ? (pl.points[index].ringNumber - 0) : "无";
            //                     item.mileageNumber = pl.points[index].mileage;
            //                     item.mileage = (pl.points[index].mileage - 0) > 0 ? pl.parseMileageFormat(pl.points[index].mileage) : "无"
            //                     //计算阶段变形
            //                     var value = 0;
            //                     if (item.value === null || data.dataList[0][index].value === null) {
            //                         value = null;
            //                     } else {
            //                         value = item.value - data.dataList[0][index].value;
            //                     }
            //                     var periodItem = angular.copy(item);
            //                     periodItem = angular.extend(periodItem, {
            //                         value: value,
            //                         time: data.dataList[0][index].time + '至' + item.time
            //                     });
            //                     return periodItem;
            //                 });
            //                 if (i === 0) {
            //                     pl.tempChangeDatas = {
            //                         dataList: [],
            //                         max: null,
            //                         min: null,
            //                         drawByMile: data.drawByMile
            //                     };
            //                 } else {
            //                     pl.tempChangeDatas.dataList.push(periodChange);
            //                     pl.tempChangeDatas.title = data.title + '-阶段变化';
            //                 }
            //             }
            //             //赋值
            //             defer.resolve(data);
            //         };
            //     })(ll));
            //     return defer.promise;
            // };
            return LocationPoints;
        }
    ]);
});
